package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.append.SearchUserAppendBo;
import com.zhuss.bean.vo.core.append.DetailUserAppendVo;
import com.zhuss.bean.vo.core.append.ListUserAppendVo;
import com.zhuss.bean.vo.core.append.MessageVo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.common.BasePageParam;
import com.zhuss.common.PageVo;
import com.zhuss.core.db.dao.ext.UserAppendDao;
import com.zhuss.core.db.entity.UserAppend;
import com.zhuss.enums.AppendTypeEnum;
import com.zhuss.enums.IsEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;


/**
 * @description: 用户关注、点赞读
 * @author: zhushanshuo
 * @create: Fri Jul 03 20:49:22 CST 2020
 **/
@Component("userAppendRead")
public class UserAppendRead {
	
	@Autowired
	private UserAppendDao userAppendDao;
	
	@Autowired
	private UserRead userRead;
	
	
	private void updateAlreadyRead(BasePageParam param, Integer appendType) {
		Long targetUserId = param.getConcurrentUserId();
		userAppendDao.updateAlreadyRead(appendType, targetUserId, IsEnum.是.key());
	}
	
	
	public Integer countNoReadNum(Long targetUserId, Integer appendType) {
		Integer noReadNum = userAppendDao.countNoReadNum(appendType, targetUserId, IsEnum.否.key());
		return noReadNum;
	}
	
	
	private PageVo<DetailUserAppendVo> listAppend(BasePageParam param, Integer appendType) {
		Integer start = param.getStart();
		Integer length = param.getLength();
		Long targetUserId = param.getConcurrentUserId();
		PageVo<DetailUserAppendVo> result = new PageVo<DetailUserAppendVo>();
		List<DetailUserAppendVo> list = userAppendDao.listByType(appendType, targetUserId, null, start, length);
		Integer total = userAppendDao.countListByType(appendType, targetUserId, null);
		result.setItems(list);
		result.setPageNum(param.getPageNum());
		result.setPageSize(param.getPageSize());
		result.setTotal(total);
		return result;
	}
	
	public MessageVo<DetailUserAppendVo> listAppend(BasePageParam param) {
		MessageVo<DetailUserAppendVo> result = new MessageVo<>();
		PageVo<DetailUserAppendVo> pageVo = listAppend(param, AppendTypeEnum.关注.key());
		Integer noReadNum = countNoReadNum(param.getConcurrentUserId(), AppendTypeEnum.关注.key());
		result.setPageVo(pageVo);
		result.setNoReadNum(noReadNum);
		updateAlreadyRead(param, AppendTypeEnum.关注.key());
		return result;
	}
	
	public List<DetailUserAppendVo> listByType(Integer type, Long targetUserId, Long creatorId) {
		List<DetailUserAppendVo> list = userAppendDao.listByType(type, targetUserId, creatorId, 0, Integer.MAX_VALUE);
		if (targetUserId == null) {
			list.stream().forEach(new Consumer<DetailUserAppendVo>() {

				@Override
				public void accept(DetailUserAppendVo t) {
					try {
						DetailUserVo detailUserVo = (DetailUserVo) userRead.detail(t.getTargetUserId());
						t.setName(detailUserVo.getName());
						t.setUsername(detailUserVo.getUsername());
					} catch (WpsException e) {
					}
				}

			});
		} else if (creatorId == null) {
			list.stream().forEach(new Consumer<DetailUserAppendVo>() {

				@Override
				public void accept(DetailUserAppendVo t) {
					try {
						DetailUserVo detailUserVo = (DetailUserVo) userRead.detail(t.getCreatorId());
						t.setName(detailUserVo.getName());
						t.setUsername(detailUserVo.getUsername());
					} catch (WpsException e) {
					}
				}

			});
		}
		return list;
	}
	
	public DetailUserAppendVo getByType(Integer type, Long targetUserId, Long creatorId) {
		DetailUserAppendVo result = userAppendDao.getByType(type, targetUserId, creatorId);
		return result;
	}
	
	
	
	/**
	 * 用户关注、点赞
	 * @return
	 */
	public Object listUserAppend(List<Long> userAppendIdList) {
		List<UserAppend> result = userAppendDao.listById(userAppendIdList);
		return result;
	}
	
	
	/**
	 * 根据名字查询用户关注、点赞
	 * @param 
	 * @return
	 */
	public List<UserAppend> list(String name) {
		List<UserAppend> result = userAppendDao.listByName(name);
		return result;
	}
	
	public List<UserAppend> listNotId(String name, Long userAppendId) {
		List<UserAppend> result = new ArrayList<UserAppend>();
		List<UserAppend> list = list(name);
		for (UserAppend c : list) {
			if (!c.getUserAppendId().equals(userAppendId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	/**
	 * 用户关注、点赞 
	 * @return
	 */
	public Object listUserAppendId(Integer area) {
		List<UserAppend> userAppendList = userAppendDao.listAll(area);
		List<Long> result = new ArrayList<>();
		transferListId(userAppendList, result);
		return result;
	}
	
	private void transferListId(List<UserAppend> userAppendList, List<Long> result) {
		for (UserAppend item : userAppendList) {
			result.add(item.getUserAppendId());
		}
	}

	/**
	 * 用户关注、点赞列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchUserAppendBo bo) {
		PageVo<ListUserAppendVo> result = new PageVo<ListUserAppendVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(userAppendDao.list(bo));
        result.setTotal(userAppendDao.countList(bo));
		return result;
	}
	
	/**
	 * 用户关注、点赞详情
	 * @param userAppendId
	 * @return
	 * @throws WpsException
	 */
	public Object detail(Long userAppendId) throws WpsException {
		Object result = userAppendDao.detail(userAppendId);
		if (result == null) {
			throw new WpsException(ExceptionCode.数据不存在, "userAppendId: " + String.valueOf(userAppendId));
		}
		return result;
	}
	
	

}
